#installing datarium
rm(list=ls())
#install.packages("datarium")
library(datarium)
## Warning: package 'datarium' was built under R version 4.2.3
#reading data set of our project
data = read.csv(file.choose())
head(data)
## Date High Low Open Close Volume Marketcap
## 1 4/29/2013 23:59 147.49 134.0000 134.444 144.54 0 1603768865
## 2 4/30/2013 23:59 146.93 134.0500 144.000 139.00 0 1542813125
## 3 05-01-2013 23:59 139.89 107.7200 139.000 116.99 0 1298954594
## 4 05-02-2013 23:59 125.60 92.2819 116.380 105.21 0 1168517495
## 5 05-03-2013 23:59 108.13 79.1000 106.250 97.75 0 1085995169
## 6 05-04-2013 23:59 115.00 92.5000 98.100 112.50 0 1250316563
#visualization for high and low
library(ggplot2)
par(mar = c(2,2,2,2))
ggplot(data,aes(x=High,y=Low))+geom_point()

#visualization for date and open
library(ggplot2)
par(mar = c(2,2,2,2))
ggplot(data,aes(x=Date,y=Open))+geom_point()

#geom smooth method for all 3 models we taken:###
#geom smooth method for high and low
ggplot(data,aes(x=High,y=Low))+geom_point()+geom_smooth(method = 'lm',se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

#visualization for open and close
library(ggplot2)
par(mar = c(2,2,2,2))
ggplot(data,aes(x=Open,y=Close))+geom_point()

#geom smooth method for close and open
ggplot(data,aes(x=Open,y=Close))+geom_point()+geom_smooth(method = 'lm',se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

#geom smooth method for date and open
ggplot(data,aes(x=Date,y=Open))+geom_point()+geom_smooth(method = 'lm',se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

#correlation for models we choosen:###\#
#correlation between high and low
cor(data$High,data$Low)
## [1] 0.9988414
#correlation between open and close
cor(data$Open,data$Close)
## [1] 0.9986457
#assinging the models :### ##model 1 for high and low #model 2 for open and close #model 3 for marketcap and open
#assigning model1 for low and high
model1 = lm(High~Low,data)
model1
##
## Call:
## lm(formula = High ~ Low, data = data)
##
## Coefficients:
## (Intercept) Low
## -46.39 1.07
#assigning model2 for open and close
model2 = lm(Open~Close,data)
model2
##
## Call:
## lm(formula = Open ~ Close, data = data)
##
## Coefficients:
## (Intercept) Close
## 3.9350 0.9978
#assigning model3 for date and open
model3 = lm(Marketcap~Open,data)
model3
##
## Call:
## lm(formula = Marketcap ~ Open, data = data)
##
## Coefficients:
## (Intercept) Open
## -4.124e+09 1.865e+07
#summary for models taken###
#summary for model1
summary(model1)
##
## Call:
## lm(formula = High ~ Low, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2965.4 -158.7 21.5 43.5 10764.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.639e+01 1.193e+01 -3.888 0.000103 ***
## Low 1.070e+00 9.429e-04 1134.774 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 560.4 on 2989 degrees of freedom
## Multiple R-squared: 0.9977, Adjusted R-squared: 0.9977
## F-statistic: 1.288e+06 on 1 and 2989 DF, p-value: < 2.2e-16
#summary for model2
summary(model2)
##
## Call:
## lm(formula = Open ~ Close, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7209.8 -29.4 -3.6 19.8 7670.5
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.935e+00 1.249e+01 0.315 0.753
## Close 9.978e-01 9.508e-04 1049.433 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 587.4 on 2989 degrees of freedom
## Multiple R-squared: 0.9973, Adjusted R-squared: 0.9973
## F-statistic: 1.101e+06 on 1 and 2989 DF, p-value: < 2.2e-16
#summary for model3
summary(model3)
##
## Call:
## lm(formula = Marketcap ~ Open, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.339e+11 -1.958e+09 1.614e+09 2.933e+09 1.387e+11
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.124e+09 2.549e+08 -16.18 <2e-16 ***
## Open 1.865e+07 1.942e+04 960.52 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.199e+10 on 2989 degrees of freedom
## Multiple R-squared: 0.9968, Adjusted R-squared: 0.9968
## F-statistic: 9.226e+05 on 1 and 2989 DF, p-value: < 2.2e-16
#evaluating the linearity assumption for every model taken###\#
#evaluating the linearity assumption among Marketcap and Open
plot(data$Marketcap,data$Open)

#evaluating the linearity assumption among Low and High
plot(data$High,data$Low)

#evaluating the linearity assumption among open and Close
plot(data$Open,data$Close)

#plotting models###\#
#plotting model1
plot(model1,6)

#plotting model2
plot(model2,3)

#plotting model3
plot(model3,3)

#histogram for all the 3 models taken :#
hist(model1$residuals)

hist(model2$residuals)

hist(model3$residuals)

#normal QQ plots for all the 3 models :#####\#
qqnorm(model1$residuals,ylab = "Residuals")
qqline(model1$residuals)

qqnorm(model2$residuals,ylab = "Residuals")
qqline(model2$residuals)

qqnorm(model3$residuals,ylab = "Residuals")
qqline(model3$residuals)

#plots for different scienorios:#####
library(graphics)
# data = read.table("coin_Bitcoin.csv",header=TRUE,sep = ",")
View(data)
par(mar=c(2,2,2,2))
plot(data$High,data$Low)

barplot(data$Open,main = "bitcoin",xlab = "index",ylab = "Open values")

hist(data$Close)

barplot(data$Open,main = "bitcoin",xlab = "High",ylab = "Low")

barplot(data$Open,main = "bitcoin",xlab = "Open",ylab = "Close")

#Forecasting using Prophet in R
#Loading the Packages
#install.packages('prophet')
#remove.packages("rlang")
#install.packages("rlang")
library(prophet)
## Warning: package 'prophet' was built under R version 4.2.3
## Loading required package: Rcpp
## Loading required package: rlang
## Warning: package 'rlang' was built under R version 4.2.3
#install.packages('tidyverse')
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.0.10 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.1.8
## ✔ purrr 1.0.1 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ purrr::%@%() masks rlang::%@%()
## ✖ dplyr::filter() masks stats::filter()
## ✖ purrr::flatten() masks rlang::flatten()
## ✖ purrr::flatten_chr() masks rlang::flatten_chr()
## ✖ purrr::flatten_dbl() masks rlang::flatten_dbl()
## ✖ purrr::flatten_int() masks rlang::flatten_int()
## ✖ purrr::flatten_lgl() masks rlang::flatten_lgl()
## ✖ purrr::flatten_raw() masks rlang::flatten_raw()
## ✖ purrr::invoke() masks rlang::invoke()
## ✖ dplyr::lag() masks stats::lag()
## ✖ purrr::splice() masks rlang::splice()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#Loading the Dataset
bitcoin = read.csv(file.choose())
head(bitcoin)
## ds y
## 1 17-09-2014 457.334
## 2 18-09-2014 424.440
## 3 19-09-2014 394.796
## 4 20-09-2014 408.904
## 5 21-09-2014 398.821
## 6 22-09-2014 402.152
#Calling the Prophet Function to Fit the Model
Model1 = prophet(bitcoin, weekly.seasonality = TRUE,daily.seasonality = TRUE)
Future1 = make_future_dataframe(Model1, periods = 365)
tail(Future1)
## ds
## 3073 0032-12-14
## 3074 0032-12-15
## 3075 0032-12-16
## 3076 0032-12-17
## 3077 0032-12-18
## 3078 0032-12-19
Forecast1 = predict(Model1, Future1)
tail(Forecast1[c('ds','yhat','yhat_lower','yhat_upper')])
## ds yhat yhat_lower yhat_upper
## 3073 0032-12-14 12286.99 -8228.479 32336.72
## 3074 0032-12-15 12224.79 -7910.187 32808.64
## 3075 0032-12-16 12195.77 -7883.456 32320.76
## 3076 0032-12-17 12222.35 -8040.417 33112.71
## 3077 0032-12-18 12115.65 -9049.486 32023.81
## 3078 0032-12-19 12127.95 -8197.613 33957.69
# show mean of predicted price and actual price of bitcoin
mean(Forecast1$yhat)
## [1] 11285.26
mean(bitcoin$y)
## [1] 11323.91
#summary of price we predicion we did.
summary(Forecast1)
## ds trend additive_terms
## Min. :0001-01-20 00:00:00.0000 Min. :1989 Min. : 6923
## 1st Qu.:0009-08-27 18:00:00.0000 1st Qu.:2099 1st Qu.: 8232
## Median :0018-04-20 00:00:00.0000 Median :2254 Median : 9072
## Mean :0018-03-06 07:05:44.9000 Mean :2255 Mean : 9030
## 3rd Qu.:0026-11-20 00:00:00.0000 3rd Qu.:2410 3rd Qu.:10183
## Max. :0032-12-19 00:00:00.0000 Max. :2558 Max. :10676
## additive_terms_lower additive_terms_upper daily daily_lower
## Min. : 6923 Min. : 6923 Min. :8977 Min. :8977
## 1st Qu.: 8232 1st Qu.: 8232 1st Qu.:8977 1st Qu.:8977
## Median : 9072 Median : 9072 Median :8977 Median :8977
## Mean : 9030 Mean : 9030 Mean :8977 Mean :8977
## 3rd Qu.:10183 3rd Qu.:10183 3rd Qu.:8977 3rd Qu.:8977
## Max. :10676 Max. :10676 Max. :8977 Max. :8977
## daily_upper weekly weekly_lower weekly_upper
## Min. :8977 Min. :-72.87875 Min. :-72.87875 Min. :-72.87875
## 1st Qu.:8977 1st Qu.:-27.59078 1st Qu.:-27.59078 1st Qu.:-27.59078
## Median :8977 Median : 1.72337 Median : 1.72337 Median : 1.72337
## Mean :8977 Mean : -0.05328 Mean : -0.05328 Mean : -0.05328
## 3rd Qu.:8977 3rd Qu.: 36.39952 3rd Qu.: 36.39952 3rd Qu.: 36.39952
## Max. :8977 Max. : 53.42449 Max. : 53.42449 Max. : 53.42449
## yearly yearly_lower yearly_upper multiplicative_terms
## Min. :-1995.37 Min. :-1995.37 Min. :-1995.37 Min. :0
## 1st Qu.: -685.39 1st Qu.: -685.39 1st Qu.: -685.39 1st Qu.:0
## Median : 92.21 Median : 92.21 Median : 92.21 Median :0
## Mean : 53.08 Mean : 53.08 Mean : 53.08 Mean :0
## 3rd Qu.: 1261.16 3rd Qu.: 1261.16 3rd Qu.: 1261.16 3rd Qu.:0
## Max. : 1653.31 Max. : 1653.31 Max. : 1653.31 Max. :0
## multiplicative_terms_lower multiplicative_terms_upper yhat_lower
## Min. :0 Min. :0 Min. :-14784
## 1st Qu.:0 1st Qu.:0 1st Qu.:-10274
## Median :0 Median :0 Median : -9170
## Mean :0 Mean :0 Mean : -9264
## 3rd Qu.:0 3rd Qu.:0 3rd Qu.: -8194
## Max. :0 Max. :0 Max. : -4976
## yhat_upper trend_lower trend_upper yhat
## Min. :27949 Min. :1989 Min. :1989 Min. : 8950
## 1st Qu.:30796 1st Qu.:2099 1st Qu.:2099 1st Qu.:10497
## Median :31877 Median :2254 Median :2254 Median :11294
## Mean :31847 Mean :2255 Mean :2255 Mean :11285
## 3rd Qu.:32933 3rd Qu.:2410 3rd Qu.:2410 3rd Qu.:12429
## Max. :36067 Max. :2558 Max. :2558 Max. :13124
#Plotting the Model Estimates
dyplot.prophet(Model1, Forecast1)
## Warning: `select_()` was deprecated in dplyr 0.7.0.
## ℹ Please use `select()` instead.
## ℹ The deprecated feature was likely used in the dplyr package.
## Please report the issue at <https://github.com/tidyverse/dplyr/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
prophet_plot_components(Model1, Forecast1)

#Forecasting using SVM in R
#Loading the Packages
#install.packages('caTools')
library(caTools)
## Warning: package 'caTools' was built under R version 4.2.3
#install.packages('caret')
library(caret)
## Warning: package 'caret' was built under R version 4.2.3
## Loading required package: lattice
##
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
##
## lift
#install.packages('kernlab')
library(kernlab)
##
## Attaching package: 'kernlab'
## The following object is masked from 'package:purrr':
##
## cross
## The following object is masked from 'package:ggplot2':
##
## alpha
#Loading the Dataset
svm = read.csv(file.choose())
head(svm)
## ds yL yH yO yC
## 1 5/27/2021 23:59 40379.62 37247.90 39316.89 38436.97
## 2 5/28/2021 23:59 38856.97 34779.04 38507.08 35697.61
## 3 5/29/2021 23:59 37234.50 33693.93 35684.16 34616.07
## 4 5/30/2021 23:59 36400.67 33520.74 34607.41 35678.13
## 5 5/31/2021 23:59 37468.25 34241.94 35658.59 37332.85
## 6 06-01-2021 23:59 37896.74 35787.09 37293.79 36684.92
svm = svm[2:5]
svm
## yL yH yO yC
## 1 40379.62 37247.90 39316.89 38436.97
## 2 38856.97 34779.04 38507.08 35697.61
## 3 37234.50 33693.93 35684.16 34616.07
## 4 36400.67 33520.74 34607.41 35678.13
## 5 37468.25 34241.94 35658.59 37332.85
## 6 37896.74 35787.09 37293.79 36684.92
## 7 38231.34 35966.31 36699.92 37575.18
## 8 39478.95 37243.97 37599.41 39208.77
## 9 39242.49 35717.72 39242.49 36894.41
## 10 37917.71 34900.41 36880.16 35551.96
## 11 36436.42 35304.58 35538.61 35862.38
## 12 36790.57 33480.64 35835.27 33560.71
## 13 34017.39 31114.44 33589.52 33472.63
## 14 37537.37 32475.87 33416.98 37345.12
## 15 38334.33 35847.59 37389.52 36702.60
## 16 37608.69 36044.45 36697.03 37334.40
## 17 37408.93 34728.19 37340.14 35552.52
## 18 39322.78 34864.11 35555.79 39097.86
## 19 40978.36 38757.28 39016.97 40218.48
## 20 41295.27 39609.47 40427.17 40406.27
## 21 40516.78 38176.03 40168.69 38347.06
## 22 39513.67 37439.67 38341.42 38053.50
## 23 38187.26 35255.85 38099.48 35787.24
## 24 36457.80 34933.06 35854.53 35615.87
## 25 36059.48 33432.08 35563.14 35698.30
## 26 35721.64 31295.94 35641.15 31676.69
## 27 33292.45 28893.62 31622.38 32505.66
## 28 34753.41 31772.63 32515.71 33723.03
## 29 35228.85 32385.21 33682.80 34662.44
## 30 35487.25 31350.88 34659.10 31637.78
## 31 32637.59 30184.50 31594.66 32186.28
## 32 34656.13 32071.76 32287.52 34649.64
## 33 35219.89 33902.08 34679.12 34434.34
## 34 36542.11 34252.48 34475.56 35867.78
## 35 36074.76 34086.15 35908.39 35040.84
## 36 35035.98 32883.78 35035.98 33572.12
## 37 33939.59 32770.68 33549.60 33897.05
## 38 34909.26 33402.70 33854.42 34668.55
## 39 35937.57 34396.48 34665.56 35287.78
## 40 35284.34 33213.66 35284.34 33746.00
## 41 35038.54 33599.92 33723.51 34235.19
set.seed(3033)
intrain = createDataPartition(y = svm$yC, p=0.7, list = FALSE)
training = svm[intrain,]
testing = svm[-intrain]
dim(training);
## [1] 29 4
dim(testing)
## NULL
anyNA(svm)
## [1] FALSE
training[["yC"]] = factor(training[["yC"]])
trctr1 = trainControl(method = "repeatedcv", number = 10, repeats = 3)
svm_Linear = train(yC~ ., data = training, method = "svmLinear",
trControl=trctr1,
preProcess = c("center","scale"),
tuneLength = 10)
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, :
## There were missing values in resampled performance measures.
svm_Linear
## Support Vector Machines with Linear Kernel
##
## 29 samples
## 3 predictor
## 29 classes: '31637.78006', '32186.27767', '32505.65982', '33472.63175', '33560.70784', '33572.11765', '33723.02898', '33746.00246', '34662.43589', '34668.5484', '35040.83725', '35287.77977', '35551.95873', '35552.51715', '35615.86927', '35678.1292', '35697.60639', '35787.24478', '35867.77774', '36684.92452', '36702.59937', '37332.85369', '37334.39953', '37575.17958', '38053.50417', '38436.96854', '39097.8609', '39208.76599', '40218.47786'
##
## Pre-processing: centered (3), scaled (3)
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 22, 26, 25, 24, 28, 27, ...
## Resampling results:
##
## Accuracy Kappa
## 0.1780612 0.07502403
##
## Tuning parameter 'C' was held constant at a value of 1
#Forecasting using ARIMA in R #Loading the Packages
library(readr)
#install.packages('mlr')
library(mlr)
## Warning: package 'mlr' was built under R version 4.2.3
## Loading required package: ParamHelpers
## Warning message: 'mlr' is in 'maintenance-only' mode since July 2019.
## Future development will only happen in 'mlr3'
## (<https://mlr3.mlr-org.com>). Due to the focus on 'mlr3' there might be
## uncaught bugs meanwhile in {mlr} - please consider switching.
##
## Attaching package: 'mlr'
## The following object is masked from 'package:caret':
##
## train
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
#install.packages('forecast')
library(forecast)
## Warning: package 'forecast' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
ARIMA = read.csv(file.choose())
head(ARIMA)
## ds yL yH yO y Volume Marketcap
## 1 6/13/2015 23:59 232.652 229.210 229.920 232.402 13305300 3316504361
## 2 6/14/2015 23:59 234.858 232.004 232.442 233.543 12165900 3333657006
## 3 6/15/2015 23:59 237.836 233.422 233.422 236.823 19912100 3381329191
## 4 6/16/2015 23:59 251.742 236.122 236.765 250.895 41612000 3582987588
## 5 6/17/2015 23:59 256.853 246.476 250.823 249.284 43858400 3560953387
## 6 6/18/2015 23:59 252.108 244.127 249.428 249.007 30980200 3557986321
summarizeColumns(ARIMA)
## name type na mean disp median mad
## 1 ds character 0 NA 9.995487e-01 NA NA
## 2 yL numeric 0 9.166689e+03 1.276753e+04 6.504583e+03 6.926896e+03
## 3 yH numeric 0 8.625309e+03 1.190724e+04 6.296250e+03 6.475004e+03
## 4 yO numeric 0 8.909889e+03 1.237458e+04 6.380023e+03 6.757858e+03
## 5 y numeric 0 8.924896e+03 1.238435e+04 6.385720e+03 6.745223e+03
## 6 Volume numeric 0 1.471399e+10 2.062869e+10 5.253777e+09 7.713413e+09
## 7 Marketcap numeric 0 1.614271e+11 2.317238e+11 1.110000e+11 1.229643e+11
## min max nlevs
## 1 1.000000e+00 1.000000e+00 2216
## 2 2.263210e+02 6.486310e+04 0
## 3 1.995670e+02 6.220896e+04 0
## 4 2.100680e+02 6.352375e+04 0
## 5 2.104950e+02 6.350346e+04 0
## 6 1.060090e+07 3.510000e+11 0
## 7 3.059461e+09 1.190000e+12 0
## using ts() function for place in time-series format
tsARIMA = ts(ARIMA$y,frequency = 4,start = c(2015,6))
## ploting the time series formatted data
plot(tsARIMA)

## using the auto.arima() function to get the optimal auto arima model
autoarima1 = auto.arima(tsARIMA)
autoarima1
## Series: tsARIMA
## ARIMA(2,1,2)(0,0,1)[4]
##
## Coefficients:
## ar1 ar2 ma1 ma2 sma1
## -0.4854 -0.9452 0.4289 0.9778 0.0756
## s.e. 0.0121 0.0095 0.0070 0.0086 0.0225
##
## sigma^2 = 445108: log likelihood = -17545.42
## AIC=35102.83 AICc=35102.87 BIC=35137.05
plot(autoarima1)

## getting the forecasted data for a period of 17 weeks == 4months
forecast1 = forecast(autoarima1, h=130)
## showing the forrecasted values
forecast1
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2570 Q2 34127.43 33272.43 34982.43 32819.81 35435.05
## 2570 Q3 33900.63 32725.15 35076.10 32102.89 35698.36
## 2570 Q4 34076.83 32621.55 35532.11 31851.17 36302.48
## 2571 Q1 34237.60 32537.59 35937.62 31637.66 36837.55
## 2571 Q2 33896.82 31980.31 35813.33 30965.78 36827.86
## 2571 Q3 33916.55 31803.49 36029.62 30684.90 37148.21
## 2571 Q4 34229.10 31914.36 36543.84 30689.01 37769.18
## 2572 Q1 34058.72 31570.28 36547.17 30252.98 37864.47
## 2572 Q2 33846.00 31207.62 36484.38 29810.94 37881.05
## 2572 Q3 34110.31 31314.69 36905.93 29834.77 38385.85
## 2572 Q4 34183.08 31234.95 37131.21 29674.31 38691.85
## 2573 Q1 33897.91 30819.73 36976.10 29190.24 38605.59
## 2573 Q2 33967.56 30760.80 37174.33 29063.24 38871.88
## 2573 Q3 34203.30 30861.56 37545.05 29092.55 39314.06
## 2573 Q4 34023.03 30560.32 37485.74 28727.27 39318.79
## 2574 Q1 33887.71 30313.87 37461.54 28421.99 39353.42
## 2574 Q2 34123.80 30431.81 37815.80 28477.39 39770.22
## 2574 Q3 34137.11 30330.33 37943.88 28315.15 39959.06
## 2574 Q4 33907.48 29998.68 37816.27 27929.49 39885.46
## 2575 Q1 34006.38 29994.01 38018.74 27869.99 40142.76
## 2575 Q2 34175.42 30055.54 38295.30 27874.61 40476.24
## 2575 Q3 33999.88 29782.13 38217.62 27549.39 40450.36
## 2575 Q4 33925.30 29614.47 38236.14 27332.45 40518.16
## 2576 Q1 34127.44 29718.06 38536.82 27383.87 40871.00
## 2576 Q2 34099.80 29595.23 38604.37 27210.65 40988.95
## 2576 Q3 33922.15 29330.50 38513.80 26899.83 40944.47
## 2576 Q4 34034.51 29353.43 38715.59 26875.42 41193.60
## 2577 Q1 34147.89 29375.31 38920.48 26848.86 41446.93
## 2577 Q2 33986.64 29129.73 38843.55 26558.64 41414.64
## 2577 Q3 33957.75 29018.74 38896.76 26404.19 41511.31
## 2577 Q4 34124.20 29099.08 39149.31 26438.95 41809.45
## 2578 Q1 34070.71 28962.78 39178.63 26258.81 41882.61
## 2578 Q2 33939.34 28753.91 39124.76 26008.91 41869.76
## 2578 Q3 34053.67 28788.32 39319.02 26001.01 42106.33
## 2578 Q4 34122.35 28776.28 39468.41 25946.24 42298.45
## 2579 Q1 33980.93 28559.60 39402.27 25689.72 42272.15
## 2579 Q2 33984.67 28488.88 39480.45 25579.58 42389.75
## 2579 Q3 34116.52 28543.47 39689.58 25593.27 42639.77
## 2579 Q4 34048.99 28401.76 39696.21 25412.30 42685.67
## 2580 Q1 33957.13 28239.21 39675.06 25212.33 42701.94
## 2580 Q2 34065.56 28274.74 39856.38 25209.27 42921.86
## 2580 Q3 34099.75 28236.08 39963.42 25132.04 43067.46
## 2580 Q4 33980.66 28048.28 39913.05 24907.86 43053.46
## 2581 Q1 34006.16 28005.09 40007.23 24828.31 43184.01
## 2581 Q2 34106.35 28034.73 40177.97 24820.60 43392.09
## 2581 Q3 34033.61 27894.23 40172.99 24644.24 43422.98
## 2581 Q4 33974.22 27769.30 40179.13 24484.61 43463.82
## 2582 Q1 34071.80 27799.50 40344.11 24479.14 43664.47
## 2582 Q2 34080.57 27741.44 40419.71 24385.71 43775.44
## 2582 Q3 33984.07 27581.23 40386.91 24191.77 43776.37
## 2582 Q4 34022.63 27555.69 40489.57 24132.30 43912.96
## 2583 Q1 34095.13 27562.96 40627.30 24105.04 44085.22
## 2583 Q2 34023.49 27428.53 40618.45 23937.37 44109.61
## 2583 Q3 33989.73 27333.34 40646.13 23809.66 44169.81
## 2583 Q4 34073.84 27354.55 40793.13 23797.57 44350.11
## 2584 Q1 34064.92 27283.58 40846.26 23693.75 44436.08
## 2584 Q2 33989.75 27148.67 40830.82 23527.23 44452.27
## 2584 Q3 34034.67 27133.29 40936.05 23479.92 44589.42
## 2584 Q4 34083.92 27121.65 41046.19 23436.04 44731.79
## 2585 Q1 34017.55 26996.46 41038.63 23279.72 44755.37
## 2585 Q2 34003.22 26924.09 41082.35 23176.62 44829.81
## 2585 Q3 34072.91 26934.63 41211.19 23155.86 44989.96
## 2585 Q4 34052.62 26856.18 41249.06 23046.62 45058.63
## 2586 Q1 33996.59 26743.68 41249.51 22904.22 45088.97
## 2586 Q2 34042.97 26732.97 41352.97 22863.29 45222.65
## 2586 Q3 34073.42 26706.14 41440.70 22806.14 45340.70
## 2586 Q4 34014.80 26591.97 41437.63 22662.56 45367.04
## 2587 Q1 34014.47 26536.48 41492.47 22577.86 45451.09
## 2587 Q2 34070.04 26536.09 41603.99 22547.86 45592.22
## 2587 Q3 34043.38 26454.49 41632.26 22437.18 45649.57
## 2587 Q4 34003.80 26361.18 41646.41 22315.43 45692.16
## 2588 Q1 34048.22 26351.30 41745.13 22276.80 45819.63
## 2588 Q2 34064.06 26312.93 41815.20 22209.72 45918.41
## 2588 Q3 34014.38 26210.43 41818.34 22079.26 45949.50
## 2588 Q4 34023.52 26166.89 41880.15 22007.84 46039.20
## 2589 Q1 34066.05 26156.23 41975.86 21969.03 46163.07
## 2589 Q2 34036.77 26074.74 41998.80 21859.89 46213.64
## 2589 Q3 34010.78 25997.38 42024.18 21755.34 46266.22
## 2589 Q4 34051.07 25985.81 42116.33 21716.33 46385.82
## 2590 Q1 34056.08 25939.21 42172.94 21642.41 46469.74
## 2590 Q2 34015.56 25848.22 42182.90 21524.69 46506.43
## 2590 Q3 34030.50 25812.68 42248.32 21462.42 46598.58
## 2590 Q4 34061.54 25792.95 42330.14 21415.82 46707.27
## 2591 Q1 34032.35 25713.88 42350.83 21310.35 46754.36
## 2591 Q2 34017.18 25649.40 42384.96 21219.77 46814.59
## 2591 Q3 34052.14 25634.67 42469.61 21178.73 46925.55
## 2591 Q4 34049.51 25582.71 42516.31 21100.65 46998.37
## 2592 Q1 34017.74 25502.49 42532.99 20994.79 47040.70
## 2592 Q2 34035.65 25471.87 42599.43 20938.48 47132.82
## 2592 Q3 34056.98 25444.56 42669.41 20885.42 47228.55
## 2592 Q4 34029.70 25369.43 42689.98 20784.95 47274.45
## 2593 Q1 34022.78 25315.03 42730.53 20705.42 47340.13
## 2593 Q2 34051.93 25296.42 42807.43 20661.54 47442.32
## 2593 Q3 34044.32 25241.47 42847.17 20581.52 47507.12
## 2593 Q4 34020.46 25170.95 42869.97 20486.30 47554.62
## 2594 Q1 34039.23 25142.95 42935.51 20433.55 47644.92
## 2594 Q2 34052.68 25109.64 42995.71 20375.49 47729.86
## 2594 Q3 34028.40 25039.30 43017.51 20280.75 47776.05
## 2594 Q4 34027.48 24992.54 43062.42 20209.74 47845.22
## 2595 Q1 34050.87 24969.92 43131.82 20162.75 47938.99
## 2595 Q2 34040.39 24913.85 43166.93 20082.55 47998.23
## 2595 Q3 34023.37 24851.76 43194.97 19996.61 48050.13
## 2595 Q4 34041.54 24824.76 43258.32 19945.69 48137.38
## 2596 Q1 34048.81 24786.96 43310.65 19884.04 48213.58
## 2596 Q2 34028.10 24721.77 43334.44 19795.29 48260.92
## 2596 Q3 34031.28 24680.60 43381.96 19730.65 48331.91
## 2596 Q4 34049.31 24654.19 43444.43 19680.72 48417.90
## 2597 Q1 34037.55 24598.41 43476.69 19601.63 48473.47
## 2597 Q2 34026.22 24543.45 43508.99 19523.57 48528.87
## 2597 Q3 34042.83 24516.35 43569.32 19473.33 48612.34
## 2597 Q4 34045.48 24475.44 43615.52 19409.36 48681.60
## 2598 Q1 34028.49 24415.38 43641.61 19326.50 48730.48
## 2598 Q2 34034.24 24378.14 43690.33 19266.51 48801.96
## 2598 Q3 34047.51 24348.40 43746.61 19214.00 48881.01
## 2598 Q4 34035.63 24293.91 43777.36 19136.95 48934.32
## 2599 Q1 34028.85 24244.80 43812.90 19065.44 48992.27
## 2599 Q2 34043.37 24216.93 43869.80 19015.13 49071.60
## 2599 Q3 34042.73 24174.11 43911.35 18949.98 49135.48
## 2599 Q4 34029.32 24118.91 43939.73 18872.66 49185.98
## 2600 Q1 34036.43 24084.29 43988.58 18815.94 49256.92
## 2600 Q2 34045.66 24051.81 44039.50 18761.39 49329.93
## 2600 Q3 34034.46 23999.26 44069.65 18686.95 49381.96
## 2600 Q4 34031.17 23954.85 44107.50 18620.76 49441.59
## 2601 Q1 34043.36 23925.87 44160.84 18570.00 49516.71
## 2601 Q2 34040.54 23882.12 44198.97 18504.57 49576.52
## 2601 Q3 34030.40 23831.35 44229.44 18432.30 49628.49
## 2601 Q4 34037.98 23798.35 44277.61 18377.82 49698.14
## 2602 Q1 34043.89 23763.75 44324.03 18321.78 49766.00
## 2602 Q2 34033.85 23713.52 44354.18 18250.27 49817.44
## 2602 Q3 34033.14 23672.78 44393.50 18188.33 49877.94
## ploting the forecasted data from the auto arima model
plot(forecast1)

## ploting the residuals over time to see congruence or varience
plot(forecast1$residuals)

### ploting the residuals (sample vs. therotical)
qqnorm(forecast1$residuals)

##
acf(forecast1$residuals)

pacf(forecast1$residuals)

### getting accuracy by mape and other leading indicators - each dataset is different method
## method - 1
summary(autoarima1)
## Series: tsARIMA
## ARIMA(2,1,2)(0,0,1)[4]
##
## Coefficients:
## ar1 ar2 ma1 ma2 sma1
## -0.4854 -0.9452 0.4289 0.9778 0.0756
## s.e. 0.0121 0.0095 0.0070 0.0086 0.0225
##
## sigma^2 = 445108: log likelihood = -17545.42
## AIC=35102.83 AICc=35102.87 BIC=35137.05
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 14.32364 666.26 277.4883 0.1383791 2.680867 0.4654142 -0.006435385
## method - 2
accuracy(autoarima1)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 14.32364 666.26 277.4883 0.1383791 2.680867 0.4654142 -0.006435385
reg = read.csv(file.choose())
head(reg)
## Date High Low Open Close Volume Marketcap
## 1 4/29/2013 23:59 147.49 134.0000 134.444 144.54 0 1603768865
## 2 4/30/2013 23:59 146.93 134.0500 144.000 139.00 0 1542813125
## 3 05-01-2013 23:59 139.89 107.7200 139.000 116.99 0 1298954594
## 4 05-02-2013 23:59 125.60 92.2819 116.380 105.21 0 1168517495
## 5 05-03-2013 23:59 108.13 79.1000 106.250 97.75 0 1085995169
## 6 05-04-2013 23:59 115.00 92.5000 98.100 112.50 0 1250316563
library(tidyverse)
library(caret)
#Split the data into training and test dataset
set.seed(123)
data1=select(reg,-c(Date))
train_sample=data1$Close %>% createDataPartition(p=0.8,list = FALSE)
train1=data1[train_sample,]
head(train1)
## High Low Open Close Volume Marketcap
## 2 146.93 134.0500 144.00 139.00 0 1542813125
## 4 125.60 92.2819 116.38 105.21 0 1168517495
## 5 108.13 79.1000 106.25 97.75 0 1085995169
## 6 115.00 92.5000 98.10 112.50 0 1250316563
## 7 118.80 107.1430 112.90 115.91 0 1288693176
## 8 124.66 106.6400 115.98 112.30 0 1249023060
test1=data1[-train_sample,]
head(test1)
## High Low Open Close Volume Marketcap
## 1 147.49 134.00 134.444 144.54 0 1603768865
## 3 139.89 107.72 139.000 116.99 0 1298954594
## 9 113.44 97.70 112.250 111.50 0 1240593600
## 17 115.81 103.50 111.400 114.22 0 1274623813
## 22 123.62 120.12 122.500 122.00 0 1363709900
## 27 133.22 128.90 133.100 131.98 0 1477958233
data1=select(reg,-c(Date))
index <- sample(2,nrow(data1),replace= TRUE,prob=c(0.8,0.2))
trainClean <- data1[index==1,]
testClean <- data1[index==2,]
model_lm <- lm(trainClean$Close~., data =trainClean)
summary(model_lm)
##
## Call:
## lm(formula = trainClean$Close ~ ., data = trainClean)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1668.64 -71.87 -32.56 58.92 1000.02
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.050e+02 4.787e+00 21.935 < 2e-16 ***
## High 5.554e-01 1.257e-02 44.200 < 2e-16 ***
## Low 2.900e-01 9.821e-03 29.526 < 2e-16 ***
## Open -2.785e-01 1.114e-02 -24.994 < 2e-16 ***
## Volume -1.621e-09 3.198e-10 -5.068 4.33e-07 ***
## Marketcap 2.300e-08 5.264e-10 43.695 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 171.6 on 2402 degrees of freedom
## Multiple R-squared: 0.9998, Adjusted R-squared: 0.9998
## F-statistic: 2.093e+06 on 5 and 2402 DF, p-value: < 2.2e-16
library(Metrics)
##
## Attaching package: 'Metrics'
## The following object is masked from 'package:forecast':
##
## accuracy
## The following objects are masked from 'package:caret':
##
## precision, recall
## The following object is masked from 'package:rlang':
##
## ll
#Make prediction
pred1 = model_lm %>% predict(testClean)
print(head(pred1))
## 12 13 15 20 25 28
## 203.7241 200.4487 202.4187 207.3700 209.3301 216.0502
#Model Performance & Root Mean Squared Error
root_mean_Sq_err1=RMSE(pred1,testClean$S1)
print("RMSE")
## [1] "RMSE"
print(root_mean_Sq_err1)
## [1] NaN
#R squared
R2_1 = R2(pred1,testClean$Close)
print("R-Squared")
## [1] "R-Squared"
print(R2_1)
## [1] 0.9996439
#Mean Squared Error
MSE1=mean((pred1 - testClean$Close)^2)
print("MSE")
## [1] "MSE"
print(MSE1)
## [1] 45197.47
#Mean Absolute Error
MAE1=mae(pred1,testClean$Close)
print("MAE")
## [1] "MAE"
print(MAE1)
## [1] 107.434
# Adjusted R-squared.
Adj_R_sq1=summary(model_lm)$adj.r.squared
print("Adjusted R squared")
## [1] "Adjusted R squared"
print(Adj_R_sq1)
## [1] 0.99977
df <- data.frame(test_L1 = testClean$Open,test_T1 = testClean$High, test_S=testClean$Close, pred_S1=pred1)
head(df)
## test_L1 test_T1 test_S pred_S1
## 12 112.799 122.00 117.200 203.7241
## 13 117.700 118.68 115.243 200.4487
## 15 114.820 118.70 117.980 202.4187
## 20 123.500 125.25 123.498 207.3700
## 25 123.800 126.93 126.700 209.3301
## 28 131.986 136.00 133.480 216.0502
#ridge
x <- model.matrix(data1$Close~., data1)
y <- data1$Close
lambda <- 10^seq(10, -2, length = 100)
#install.packages('glmnet')
library(glmnet)
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
## Loaded glmnet 4.1-4
ridge.mod <- glmnet(x, y, alpha = 0, lambda = lambda)
coef.glmnet(ridge.mod)
## 7 x 100 sparse Matrix of class "dgCMatrix"
## [[ suppressing 100 column names 's0', 's1', 's2' ... ]]
##
## (Intercept) 6.711254e+03 6.711243e+03 6.711228e+03 6.711207e+03 6.711181e+03
## (Intercept) . . . . .
## High 1.095542e-06 1.448235e-06 1.914478e-06 2.530821e-06 3.345586e-06
## Low 1.173388e-06 1.551144e-06 2.050517e-06 2.710655e-06 3.583315e-06
## Open 1.129101e-06 1.492600e-06 1.973126e-06 2.608349e-06 3.448072e-06
## Volume 5.455924e-13 7.212392e-13 9.534336e-13 1.260379e-12 1.666141e-12
## Marketcap 6.049238e-14 7.996723e-14 1.057117e-13 1.397443e-13 1.847330e-13
##
## (Intercept) 6.711145e+03 6.711098e+03 6.711037e+03 6.710955e+03 6.710847e+03
## (Intercept) . . . . .
## High 4.422650e-06 5.846451e-06 7.728607e-06 1.021667e-05 1.350566e-05
## Low 4.736910e-06 6.261880e-06 8.277772e-06 1.094262e-05 1.446530e-05
## Open 4.558127e-06 6.025538e-06 7.965341e-06 1.052960e-05 1.391931e-05
## Volume 2.202528e-12 2.911592e-12 3.848918e-12 5.087981e-12 6.725903e-12
## Marketcap 2.442049e-13 3.228223e-13 4.267482e-13 5.641293e-13 7.457340e-13
##
## (Intercept) 6.710704e+03 6.710515e+03 6.710266e+03 6.709936e+03 6.709500e+03
## (Intercept) . . . . .
## High 1.785338e-05 2.360060e-05 3.119768e-05 4.123989e-05 5.451390e-05
## Low 1.912193e-05 2.527746e-05 3.341427e-05 4.416987e-05 5.838677e-05
## Open 1.840015e-05 2.432331e-05 3.215291e-05 4.250238e-05 5.618238e-05
## Volume 8.891059e-12 1.175313e-11 1.553636e-11 2.053714e-11 2.714713e-11
## Marketcap 9.857959e-13 1.303128e-12 1.722596e-12 2.277061e-12 3.009949e-12
##
## (Intercept) 6.708923e+03 6.708162e+03 6.707155e+03 6.705824e+03 6.704066e+03
## (Intercept) . . . . .
## High 7.205926e-05 9.524953e-05 1.258993e-04 1.664053e-04 2.199323e-04
## Low 7.717829e-05 1.020154e-04 1.348413e-04 1.782224e-04 2.355475e-04
## Open 7.426409e-05 9.816277e-05 1.297480e-04 1.714887e-04 2.266448e-04
## Volume 3.588382e-11 4.743090e-11 6.269143e-11 8.285791e-11 1.095045e-10
## Marketcap 3.978641e-12 5.258945e-12 6.950994e-12 9.187020e-12 1.214158e-11
##
## (Intercept) 6.701744e+03 6.698675e+03 6.694623e+03 6.689273e+03 6.682212e+03
## (Intercept) . . . . .
## High 2.906577e-04 3.840929e-04 5.075047e-04 6.704662e-04 8.855749e-04
## Low 3.112890e-04 4.113469e-04 5.434991e-04 7.179897e-04 9.482953e-04
## Open 2.995181e-04 3.957830e-04 5.229184e-04 6.907726e-04 9.122975e-04
## Volume 1.447083e-10 1.912081e-10 2.526127e-10 3.336717e-10 4.406278e-10
## Marketcap 1.604501e-11 2.120105e-11 2.800994e-11 3.699852e-11 4.885932e-11
##
## (Intercept) 6.672898e+03 6.660658e+03 6.644518e+03 6.623301e+03 6.595458e+03
## (Intercept) . . . . .
## High 1.169384e-03 1.541424e-03 2.032834e-03 2.678880e-03 3.526738e-03
## Low 1.252117e-03 1.650970e-03 2.177311e-03 2.869284e-03 3.777424e-03
## Open 1.204498e-03 1.588642e-03 2.095104e-03 2.760937e-03 3.634762e-03
## Volume 5.816706e-10 7.672225e-10 1.011634e-09 1.332822e-09 1.754107e-09
## Marketcap 6.450100e-11 8.511339e-11 1.122480e-10 1.479213e-10 1.947386e-10
##
## (Intercept) 6.559006e+03 6.511427e+03 6.449571e+03 6.369568e+03 6.266780e+03
## (Intercept) . . . . .
## High 4.636900e-03 6.086157e-03 7.970689e-03 1.040880e-02 1.354244e-02
## Low 4.966532e-03 6.518875e-03 8.537499e-03 1.114916e-02 1.450599e-02
## Open 4.778920e-03 6.272551e-03 8.214774e-03 1.072750e-02 1.395701e-02
## Volume 2.305319e-09 3.024191e-09 3.957752e-09 5.163442e-09 6.709478e-09
## Marketcap 2.560401e-10 3.360668e-10 4.401302e-10 5.747639e-10 7.478087e-10
##
## (Intercept) 6.135843e+03 5.970856e+03 5.765791e+03 5.515211e+03 5.215302e+03
## (Intercept) . . . . .
## High 1.753623e-02 2.257190e-02 2.883651e-02 3.650096e-02 4.568963e-02
## Low 1.878444e-02 2.417951e-02 3.089180e-02 3.910499e-02 4.895321e-02
## Open 1.807291e-02 2.326259e-02 2.971855e-02 3.761684e-02 4.708538e-02
## Volume 8.673730e-09 1.114006e-08 1.419080e-08 1.789428e-08 2.228691e-08
## Marketcap 9.683578e-10 1.246459e-09 1.592443e-09 2.015768e-09 2.523324e-09
##
## (Intercept) 4.865145e+03 4.467973e+03 4.032001e+03 3.570341e+03 3.099804e+03
## (Intercept) . . . . .
## High 5.644276e-02 6.867931e-02 8.216942e-02 9.654776e-02 1.113386e-01
## Low 6.048071e-02 7.360201e-02 8.807703e-02 1.035122e-01 1.194004e-01
## Open 5.816502e-02 7.077108e-02 8.467007e-02 9.947738e-02 1.146999e-01
## Volume 2.735120e-08 3.299383e-08 3.903126e-08 4.518789e-08 5.111697e-08
## Marketcap 3.117363e-09 3.793446e-09 4.539114e-09 5.334020e-09 6.152062e-09
##
## (Intercept) 2.638542e+03 2.203551e+03 1.808011e+03 1.460688e+03 1.164787e+03
## (Intercept) . . . . .
## High 1.260202e-01 1.401369e-01 1.533091e-01 1.653124e-01 1.759790e-01
## Low 1.352013e-01 1.504024e-01 1.646370e-01 1.776199e-01 1.892737e-01
## Open 1.298135e-01 1.443138e-01 1.578420e-01 1.701387e-01 1.811823e-01
## Volume 5.643982e-08 6.078449e-08 6.384401e-08 6.539719e-08 6.535681e-08
## Marketcap 6.965004e-09 7.747263e-09 8.478702e-09 9.149235e-09 9.755129e-09
##
## (Intercept) 9.189552e+02 7.196880e+02 5.613736e+02 4.374899e+02 3.429375e+02
## (Intercept) . . . . .
## High 1.854943e-01 1.938672e-01 2.012540e-01 2.078715e-01 2.138665e-01
## Low 1.997500e-01 2.091035e-01 2.175002e-01 2.252344e-01 2.319424e-01
## Open 1.909546e-01 1.995641e-01 2.070919e-01 2.135709e-01 2.188467e-01
## Volume 6.368075e-08 6.050858e-08 5.605387e-08 5.060512e-08 4.453657e-08
## Marketcap 1.029368e-08 1.077186e-08 1.119446e-08 1.155946e-08 1.189499e-08
##
## (Intercept) 2.714355e+02 2.183666e+02 1.788862e+02 1.493403e+02 1.273436e+02
## (Intercept) . . . . .
## High 2.193971e-01 2.246937e-01 2.293160e-01 2.329298e-01 2.357181e-01
## Low 2.378498e-01 2.426437e-01 2.463047e-01 2.494900e-01 2.520520e-01
## Open 2.228890e-01 2.254105e-01 2.269204e-01 2.280050e-01 2.285457e-01
## Volume 3.822748e-08 3.203886e-08 2.639501e-08 2.134682e-08 1.701526e-08
## Marketcap 1.219933e-08 1.249770e-08 1.278982e-08 1.305262e-08 1.329898e-08
##
## (Intercept) 1.110333e+02 9.879889e+01 9.009840e+01 8.377404e+01 7.943306e+01
## (Intercept) . . . . .
## High 2.378237e-01 2.394607e-01 2.404343e-01 2.408541e-01 2.406845e-01
## Low 2.540476e-01 2.554005e-01 2.565796e-01 2.575106e-01 2.582313e-01
## Open 2.285204e-01 2.280049e-01 2.269332e-01 2.252779e-01 2.227927e-01
## Volume 1.338984e-08 1.049406e-08 8.035091e-09 6.103699e-09 4.588475e-09
## Marketcap 1.353528e-08 1.376040e-08 1.399488e-08 1.423933e-08 1.452073e-08
##
## (Intercept) 7.707862e+01 7.637808e+01 7.680559e+01 7.829293e+01 8.068576e+01
## (Intercept) . . . . .
## High 2.396870e-01 2.382325e-01 2.367121e-01 2.350763e-01 2.333663e-01
## Low 2.587472e-01 2.587860e-01 2.583019e-01 2.573127e-01 2.558253e-01
## Open 2.184440e-01 2.119891e-01 2.040101e-01 1.942498e-01 1.827912e-01
## Volume 3.399799e-09 2.472968e-09 1.751595e-09 1.186028e-09 7.403538e-10
## Marketcap 1.491774e-08 1.544585e-08 1.606242e-08 1.678858e-08 1.762154e-08
##
## (Intercept) 8.378028e+01 8.741672e+01 9.153457e+01 9.580123e+01 1.000201e+02
## (Intercept) . . . . .
## High 2.316296e-01 2.298927e-01 2.281989e-01 2.266721e-01 2.253429e-01
## Low 2.538724e-01 2.514792e-01 2.486134e-01 2.454253e-01 2.420474e-01
## Open 1.700075e-01 1.562639e-01 1.416754e-01 1.271461e-01 1.132206e-01
## Volume 3.875138e-10 1.050106e-10 -1.271467e-10 -3.181308e-10 -4.766200e-10
## Marketcap 1.854028e-08 1.952503e-08 2.057101e-08 2.161652e-08 2.262494e-08
##
## (Intercept) 1.040899e+02 1.077425e+02 1.109885e+02 1.138275e+02
## (Intercept) . . . .
## High 2.242251e-01 2.233460e-01 2.226560e-01 2.221309e-01
## Low 2.385724e-01 2.352558e-01 2.321567e-01 2.293269e-01
## Open 1.001486e-01 8.863461e-02 7.859075e-02 6.994830e-02
## Volume -6.102178e-10 -7.202030e-10 -8.116105e-10 -8.877565e-10
## Marketcap 2.357819e-08 2.442428e-08 2.516811e-08 2.581239e-08
##
## (Intercept) 1.161767e+02 1.181259e+02 1.196762e+02 1.209122e+02
## (Intercept) . . . .
## High 2.217530e-01 2.214784e-01 2.212853e-01 2.211469e-01
## Low 2.268931e-01 2.248134e-01 2.231166e-01 2.217382e-01
## Open 6.287347e-02 5.706586e-02 5.247735e-02 4.884291e-02
## Volume -9.490521e-10 -9.987689e-10 -1.037850e-09 -1.068686e-09
## Marketcap 2.634283e-08 2.678039e-08 2.712752e-08 2.740341e-08
##
## (Intercept) 1.218332e+02 1.226034e+02 1.232195e+02 1.236795e+02
## (Intercept) . . . .
## High 2.210514e-01 2.209765e-01 2.209214e-01 2.208832e-01
## Low 2.206921e-01 2.198125e-01 2.191037e-01 2.185692e-01
## Open 4.613734e-02 4.389607e-02 4.211276e-02 4.078098e-02
## Volume -1.091667e-09 -1.110594e-09 -1.125617e-09 -1.136843e-09
## Marketcap 2.760943e-08 2.778048e-08 2.791678e-08 2.801867e-08
##
## (Intercept) 1.239822e+02 1.242913e+02 1.245223e+02 1.246746e+02
## (Intercept) . . . .
## High 2.208591e-01 2.208349e-01 2.208180e-01 2.208074e-01
## Low 2.182114e-01 2.178525e-01 2.175827e-01 2.174024e-01
## Open 3.989544e-02 3.901195e-02 3.835101e-02 3.791110e-02
## Volume -1.144341e-09 -1.151770e-09 -1.157311e-09 -1.161005e-09
## Marketcap 2.808651e-08 2.815424e-08 2.820491e-08 2.823865e-08
##
## (Intercept) 1.248297e+02 1.249867e+02 1.251449e+02 1.253038e+02
## (Intercept) . . . .
## High 2.207968e-01 2.207867e-01 2.207773e-01 2.207691e-01
## Low 2.172219e-01 2.170412e-01 2.168602e-01 2.166790e-01
## Open 3.747181e-02 3.703337e-02 3.659592e-02 3.615959e-02
## Volume -1.164687e-09 -1.168340e-09 -1.171965e-09 -1.175565e-09
## Marketcap 2.827234e-08 2.830595e-08 2.833946e-08 2.837285e-08
##
## (Intercept) 1.254632e+02 1.256226e+02 1.257819e+02 1.259409e+02
## (Intercept) . . . .
## High 2.207622e-01 2.207567e-01 2.207528e-01 2.207504e-01
## Low 2.164977e-01 2.163162e-01 2.161345e-01 2.159527e-01
## Open 3.572447e-02 3.529061e-02 3.485807e-02 3.442688e-02
## Volume -1.179143e-09 -1.182704e-09 -1.186252e-09 -1.189788e-09
## Marketcap 2.840610e-08 2.843921e-08 2.847216e-08 2.850496e-08
##
## (Intercept) 1.260996e+02
## (Intercept) .
## High 2.207498e-01
## Low 2.157708e-01
## Open 3.399705e-02
## Volume -1.193316e-09
## Marketcap 2.853759e-08
#Ridge
set.seed(489)
train = sample(1:nrow(x), nrow(x)/2)
test = (-train)
ytest = y[test]
ridge.mod <- glmnet(x[train,], y[train], alpha = 0, lambda = lambda)
#find the best lambda from our list via cross-validation
cv.out <- cv.glmnet(x[train,], y[train], alpha = 0)
bestlam <- cv.out$lambda.min
#make predictions
pred2 <- predict(ridge.mod, s = bestlam, newx = x[test,])
head(pred2)
## s1
## 2 302.5993
## 3 290.0968
## 7 279.0104
## 9 274.6427
## 10 277.8563
## 13 281.4827
#Model Performance & Root Mean Squared Error
root_mean_Sq_err2=RMSE(pred2,ytest)
print("RMSE")
## [1] "RMSE"
print(root_mean_Sq_err2)
## [1] 440.9633
#R squared
R2_2 = R2(pred2,ytest)
print("R-Squared")
## [1] "R-Squared"
print(R2_2)
## s1
## [1,] 0.9990876
#Mean Squared Error
MSE2=mean((pred2 - ytest)^2)
print("MSE")
## [1] "MSE"
print(MSE2)
## [1] 194448.6
#Mean Absolute Error
library(Metrics)
MAE2=mae(pred2,ytest)
print("MAE")
## [1] "MAE"
print(MAE2)
## [1] 248.5774
plot(ytest, pred2, main ='Predicted Vs Actual')
